package io.renren.modules.bug.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import io.renren.modules.bug.service.BugService;
import io.renren.modules.bug.vo.BugResVo;
import io.renren.modules.bug.vo.BugVo;
import io.renren.modules.bug.vo.ExportBugReqVo;
import io.renren.modules.bug.vo.ExportBugVo;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-07-29 17:21:53
 */
@RestController
@RequestMapping("/bug")
@Api(tags = "Bug管理接口")
public class BugController extends AbstractController {
    @Autowired
    private BugService bugService;
    /**
     * 复合查询获取bug信息
     */
    @GetMapping("/list")
    @ApiOperation("复合查询缺陷信息")
    @ApiResponse(code = 0,message = "请求成功")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("bug:list")
    public R list(@RequestParam(required = false) Map<String, Object> params, @ApiParam(value = "bug状态") Integer status,
                  @ApiParam(value = "产品Id") Long productId,@ApiParam(value = "模块Id") Long moduleId){
        System.out.println(params);
        PageUtils page = bugService.compoundQueryBugInfoPage(params,status,productId,moduleId);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{bugId}/{rmbId}")
    @ApiOperation("回显缺陷信息")
    @ApiResponse(code = 0,message = "请求成功")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("bug:info")
    public R info(@PathVariable("bugId") Long bugId,@PathVariable("rmbId") Long rmbId){
        BugResVo bugResVo =  bugService.infoById(bugId,rmbId);
        return R.ok().put("bug", bugResVo);
    }

    /**
     * 新建或更改
     */
    @PostMapping("/save")
    @ApiOperation("新建或更改缺陷")
    @ApiResponse(code = 0,message = "请求成功")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("bug:save")
    public R saveOrUpdate(@RequestBody BugVo bugVo){
        System.out.println("前端修改功能 ===============>" + bugVo);
		bugService.saveOrUpdate(bugVo);
        return R.ok();
    }
    /**
     * 修改bug状态
     */
    @PutMapping("/updatestatus")
    @ApiOperation("修改缺陷状态")
    @ApiResponse(code = 0,message = "请求成功")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("bug:updatestatus")
    public R update(@ApiParam(value = "缺陷ID",required = true) Long bugId,
                    @ApiParam(value = "缺陷状态",required = true)Integer status){
        bugService.updateStatus(bugId,status);
        return R.ok();
    }
    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除缺陷")
    @ApiResponse(code = 0,message = "请求成功")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("bug:delete")
    public R delete(@PathVariable(value = "id") @ApiParam(value = "缺陷ID",required = true) Long bugId){
        bugService.deleteById(bugId);
        return R.ok();
    }
    /**
     * 根据开发人员Id，缺陷状态，产品id，模块id，开始时间，结束时间导出Excel
     */
    @PostMapping("/export")
    @ApiOperation("导出Excel")
    @ApiResponse(code = 0,message = "请求成功")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("bug:export")
    public R exportExcel(@RequestBody ExportBugReqVo exportBugReqVo, HttpServletResponse response) throws IOException {
        System.out.println(exportBugReqVo);
        List<ExportBugVo> list = bugService.exportExcelInfo(exportBugReqVo);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("example.xlsx", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream(), ExportBugVo.class);
        ExcelWriterSheetBuilder sheetBuilder = writerBuilder.sheet();
        WriteSheet sheet = sheetBuilder.build();
        writerBuilder.sheet().doWrite(list);
        writerBuilder.build().finish();
        return R.ok();
    }
    /**
     * 根据测试人员信息返回模块信息和产品信息
     */
/*    @GetMapping("/getExportInfo/{testid}")
    @ApiOperation("导出Excel回显信息")
    @ApiResponse(code = 0,message = "请求成功")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("bug:export")
    public R exportExcel(@PathVariable(value = "testid") @ApiParam(value = "测试人员Id",required = true) Long testId){
        List<ExportBugReqVo> exportBugReqVos = bugService.getExportInfo(testId);
        return R.ok().put("list",exportBugReqVos);
    }*/
}
